﻿using MongoDB.Driver;
using Server.Business.proto;
using Server.Database;
using Server.Resource;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace Server.Business
{
    public class ResourceProvider
    {
        private static ResArray _resArray;
        private static MongoDatabase _database;

        static ResourceProvider()
        {
            _resArray = ResArray.ParseFrom(GetStaticResource());
        }

        public static ResArray ResArray
        {
            get
            {
                return _resArray;
            }
        }

        private static byte[] GetStaticResource()
        {
            ResArray.Builder builder = new ResArray.Builder();
            ResItemFactory factory = new ResItemFactory();
            foreach (ResItemDB itemDB in factory.Create())
            {
                ResItem.Builder itemBuilder = new ResItem.Builder();
                itemBuilder.SetId(itemDB.TeamID);
                itemBuilder.SetItemID(itemDB.ItemID);
                itemBuilder.SetItemName(itemDB.ItemName);
                builder.ResList.Add(itemBuilder.Build());
            }

            return builder.Build().ToByteArray();
        }
        
        private IEnumerable<ResItem> _productTypes;
        public IEnumerable<ResItem> ProductTypes
        {
            get
            {
                if (_productTypes == null)
                    _productTypes = GetResItems(1);
                return _productTypes;
            }
        }

        private IEnumerable<ResItem> _useArea;
        public IEnumerable<ResItem> UseArea
        {
            get
            {
                if (_useArea == null)
                    _useArea = GetResItems(2);
                return _useArea;
            }
        }


        private IEnumerable<ResItem> _orgin;
        public IEnumerable<ResItem> Orgin
        {
            get
            {
                if (_orgin == null)
                    _orgin = GetResItems(3);
                return _orgin;
            }
        }

        private IEnumerable<ResItem> _defendType;
        public IEnumerable<ResItem> ImpactType
        {
            get
            {
                if (_defendType == null)
                    _defendType = GetResItems(4);
                return _defendType;
            }
        }

        private IEnumerable<ResItem> _lightingExplain;
        public IEnumerable<ResItem> LightingExplain
        {
            get
            {
                if (_lightingExplain == null)
                    _lightingExplain = GetResItems(5);
                return _lightingExplain;
            }
        }

        private IEnumerable<ResItem> _workVoltage;
        public IEnumerable<ResItem> WorkVoltage
        {
            get
            {
                if (_workVoltage == null)
                    _workVoltage = GetResItems(6);
                return _workVoltage;
            }
        }

        private IEnumerable<ResItem> _controlType;
        public IEnumerable<ResItem> ControlType
        {
            get
            {
                if (_controlType == null)
                    _controlType = GetResItems(7);
                return _controlType;
            }
        }

        private IEnumerable<ResItem> _lightingType;
        public IEnumerable<ResItem> LightingType
        {
            get
            {
                if (_lightingType == null)
                    _lightingType = GetResItems(8);
                return _lightingType;
            }
        }

        private IEnumerable<ResItem> _setupType;
        public IEnumerable<ResItem> SetupType
        {
            get
            {
                if (_setupType == null)
                    _setupType = GetResItems(9);
                return _setupType;
            }
        }

        public IEnumerable<ResItem> GetResItems(int guid)
        {
            return from r in _resArray.ResList
                   where r.Id == guid
                   orderby r.ItemID
                   select r;
        }
    }
}
